home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: A problem with fprintf
- Date: Tue, 09 Jan 96 14:34:26 GMT
- Organization: none
- Distribution: world
- Message-ID: <821198066snz@genesis.demon.co.uk>
- References: <2154942365.28352469@ortel.org>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <2154942365.28352469@ortel.org>
- Ray_Robert@ortel.org "Ray Robert" writes:
-
- >vishwajit@cis.ufl.edu writes:
- >
- >> I am experiencing a peculiar problem with fprintf.
- >
- >> I have a piece of code where the following statement fits in:
- >
- >fprintf(fp, "%s\n", src);
- >>
- >>fp is a file-pointer and src is a static array of characters. When the
- >> code is executed repeatedly, it fails at the above statement. The error
- >> message from the debugger is as follows:
- >
- >> signal SEGV (segmentation violation) in malloc at 0xef778634
- >> malloc+0x150: st %o0, [%l5]
-
- You have probably corrupted your heap before the fprintf call.
-
- >> fp is pointing to a file that has been opened and is not the problem.
- >> Does somebody have a clue?
- >
- >Apparently your system's fprintf is doing an malloc() to set up a space in
- >which to construct the formatted string (wow! what an inefficiency there).
-
- Unlikely. More likely it allocates the stream buffer the first time the
- stream is read/written. It didn't do it when the stream was opened because
- at that point the program has the opportunity to set the buffering type
- and buffer size with setbuf/setvbuf.
-
- >It is probably incorrectly determining the size of "src", which it expects to
- >be a (pointer to a) null-terminated string. One of 2 things is happening (I
- >leave it to the academics out there to tell us which is ANSI C):
- >
- >(1) The array is being passed by value, i.e., the literal value of the first
- >element is being passed to fprintf and hence to malloc, but it's being
- >interpretted as if it's a pointer.
-
- The 'value' of an array rvalue is a pointer to its first element. This isn't
- something that a compiler is likely to get wrong.
-
- >(2) The last element of the array is not a null ('\0' in the old style, 0 in
- >the new).
-
- What styles are you referring to? '\0' and 0 both represent an int constant
- with value 0. Stylistically is it sensible to use '\0' to represent a
- value that is conceptually a character.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-